// 引入接口文件
import {queryUserRegion,getUserDetail} from './giftRegistrationListServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import Cookie from 'js-cookie'
import config from 'config/config'

import { calculate_object_name } from 'utils/upload'
import { makeAddress } from "utils/util"

// 初始默认状态
const defultState = {
	regionList:[],
	levelInfoList:[
		{name:'集团',value:'3',code:'1001'},
		{name:'区域',value:'2',code:'1002'},
		{name:'项目',value:'1',code:'1004'},
	],
	userLevel: '0',
	biz: {
		queryForm: {},
		selectedRows: []
	  },
}

// 声明module
const tmpModule = {
	// 默认状态
	state: cloneDeep(defultState),
	// 入口函数(先执行)
	subscriptions: {
		setup({ dispatch, history }) {
			history.listen(({ pathname, query }) => {
				// 页面路由匹配时执行
				if ('' + tmpModule.pathname === '' + pathname) {
					// 执行业务操作
					// dispatch({
					// 	type: 'queryInfo',
					// 	payload: {}
					// }) // end dispatch
					// accountId = query
					dispatch({ type: 'getUserDetail', payload: {} })
					dispatch({ type: 'queryUserRegion', payload: {} })
				} // end if
			}) // end listen
		},
	},

	// 方法列表
	effects: {
		*queryUserRegion({ payload }, { put, call, select }){
			try {
				let accountId = JSON.parse(localStorage.getItem('navBar')).accountId || ""
				let {success,rows} = yield call(queryUserRegion, {accountId})
				if(success){
					let regionList = rows
					regionList.forEach((item,index)=>{
						item.value = item.regionId
						item.label = item.regionName
						item.key = index
					})
					// debugger
					yield put({ type: 'updateStore', payload: { regionList:regionList } })
				}
			}catch(e){

			}
			
		},
		//获取用户详情
		*getUserDetail({ payload }, { call, put, select }){
			let isAdmin = JSON.parse(localStorage.getItem('navBar')).accountId || ''
			//账户是admin就直接返回 集团级权限
			if (isAdmin == '1') {
				yield put({ type: 'updateStore', payload: { userLevel:3}})
			}else {
				let accountId = Cookie.get(config.cookie.userId);
				const { success,data} = yield call(getUserDetail,{accountId})
				if(success){
				//编辑礼品 1项目 2区域 3集团
				//用户权限 
					var roleLevel = []
					data.roleList.forEach(item=>{
						roleLevel.push(item.permissionRoleId)
					})
					let maxLevel = Math.min(...roleLevel)
					for (let item of defultState.levelInfoList) {
						if (Number(item.code) == maxLevel ) {
							defultState.userLevel = item.value
							break
						}
					}
					yield put({ type: 'updateStore', payload: { userLevel:defultState.userLevel } })
				}
			}
		  },
		// *getRegion({ payload }, { put, call, select }) {
		// 	try {
		// 		let {success,data} = yield call(getRegion, {...payload.params })
		// 		if (data.success) {
		// 			// let regionList = data.rows
		// 			// regionList.forEach((item,index)=>{
		// 			// 	item.value = item.regionId
		// 			// 	item.label = item.regionName
		// 			// 	item.key = index
		// 			// })
		// 			yield put({ type: 'updateStore', payload: { regionList:data } })
		// 		}
		// 	} catch (e) {
		// 	}
		// },
		// *getProject({ payload }, { put, call, select }) {
		// 	try {
		// 		const data = yield call(getProject, {...payload })
		// 		if (data.success) {
		// 			let projectList = data.rows
		// 			projectList.forEach((item,index)=>{
		// 				item.value = item.projectId
		// 				item.label = item.projectName
		// 				item.key = index
		// 			})
		// 			yield put({ type: 'updateStore', payload: { projectList } })
		// 		}
		// 	} catch (e) {
		// 	}
		// },
	},

	// 存入状态机
	reducers: {
		updateStore(preState, action) {
			return Object.assign({}, preState, action.payload)
		},
	}
}

export default tmpModule
